//Copyright 2010 Balazs Keri, Imre Keri
//
//This file is part of FireworksCreator.
//
//FireworksCreator is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//FireworksCreator is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with FireworksCreator.  If not, see <http://www.gnu.org/licenses/>.
//
#ifndef _RANGE_H
#define _RANGE_H
//
#include <global.h>
#include <QMetaType>
//
class Range
{
public:
   Range():
      min(0.0),max(-1.0)
   {;}
   Range(double p_value):
      min(p_value),max(p_value)
   {;}
   Range(double p_min,double p_max):
      min(p_min),max(p_max)
   {;}
   Range(const Range &p_other):
      min(p_other.min),max(p_other.max)
   {;}
   bool is_null()const;
   bool is_empty()const;
   double get_min()const;
   double get_max()const;
   friend bool operator==(const Range &p_range1,const Range &p_range2);
   friend Range operator+(const Range &p_range1,const Range &p_range2);
   //friend Range operator-(const Range &p_range1,const Range &p_range2);
   friend Range operator*(const Range &p_range,double p_factor);
   friend Range operator*(double p_factor,const Range &p_range);
   char is_before(const Range &p_other)const;
   friend QDataStream &operator<<(QDataStream &p_stream,const Range &p_range);
   friend QDataStream &operator>>(QDataStream &p_stream,Range &p_range);
private:
   double min;
   double max;
};
Q_DECLARE_METATYPE(Range)
//
inline bool Range::is_null()const
{
   return min>max;
}
inline bool Range::is_empty()const
{
   return min==max;
}
inline double Range::get_min()const
{
   return min;
}
inline double Range::get_max()const
{
   return max;
}
inline bool operator==(const Range &p_range1,const Range &p_range2)
{
   if(p_range1.is_null()||p_range2.is_null())
      return p_range1.is_null()==p_range2.is_null();
   else
      return p_range1.min==p_range2.min&&p_range1.max==p_range2.max;
}
inline Range operator+(const Range &p_range1,const Range &p_range2)
{
   if(p_range1.is_null())
      return p_range2;
   else if(p_range2.is_null())
      return p_range1;
   else
      return Range(p_range1.min+p_range2.min,p_range1.max+p_range2.max);
}
inline Range operator*(const Range &p_range,double p_factor)
{
   if(p_range.is_null())
      return Range();
   if(p_factor>=0)
      return Range(p_range.min*p_factor,p_range.max*p_factor);
   else
      return Range(p_range.max*p_factor,p_range.min*p_factor);
}
inline Range operator*(double p_factor,const Range &p_range)
{
   return p_range*p_factor;
}
//
#endif
